package com.document.changeSkin;

import com.document.R;

import android.app.Activity; 
import android.content.SharedPreferences; 
  
public class SkinSettingManager { 
 
 
    public final static String SKIN_PREF = "skinSetting"; 
     
    public SharedPreferences skinSettingPreference; 
     
    private int[] skinResources = { R.drawable.background_1,R.drawable.background_4,
    		R.drawable.background_3, R.drawable.background_2, R.drawable.background_5
    }; 
     
    private Activity mActivity; 
     
     
    public SkinSettingManager(Activity activity) { 
        this.mActivity = activity;   
        skinSettingPreference = mActivity.getSharedPreferences(SKIN_PREF, 3); 
    } 
     // 获取当前程序的皮肤序号   
    public int getSkinType() { 
        String key = "skin_type"; 
        return skinSettingPreference.getInt(key, 0); 
    } 
 
   
     // 把皮肤序号写到全局设置里去
    public void setSkinType(int j) { 
        SharedPreferences.Editor editor = skinSettingPreference.edit(); 
        String key  = "skin_type"; 
         
        editor.putInt(key, j); 
        editor.commit(); 
    } 
     
   
     // 获取当前皮肤的背景图资源id
  
    public int getCurrentSkinRes() { 
        int skinLen = skinResources.length; 
        int getSkinLen = getSkinType(); 
        if(getSkinLen >= skinLen){ 
            getSkinLen = 0; 
        } 
         
        return skinResources[getSkinLen]; 
    } 
     
    public void toggleSkins(){ 
         
        int skinType = getSkinType(); 
        if(skinType == skinResources.length - 1){ 
            skinType = 0; 
        }else{           
            skinType ++; 
        } 
        setSkinType(skinType); 
        mActivity.getWindow().setBackgroundDrawable(null); 
        try { 
            mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes()); 
        } catch (Throwable e) { 
            e.printStackTrace(); 
 
        } 
         
         
    } 
         
    // 用于初始化皮肤 
    public void initSkins(){     
        mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes()); 
    } 
 
    /// 随即切换一个背景皮肤
    
    public void changeSkin(int id) { 
         
        setSkinType(id); 
        mActivity.getWindow().setBackgroundDrawable(null); 
        try { 
            mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes()); 
        } catch (Throwable e) { 
            e.printStackTrace(); 
        } 
    } 
 
} 
